package com.test.list;

public class LikeList implements List {
    //头节点
    private Node head=new Node();
    //索引数
    private int size;

    @Override
    public int size() {
        return size;
    }

    public LikeList() {
    }

    @Override
    public Object get(int i) {
        Node p=head;
        for(int j=0;j<=i;j++){
            p=p.next;
        }
        return p.e;
    }

    @Override
    public boolean isEmpty() {
        return size==0?true:false;
    }

    @Override
    public boolean contains(Object e) {
        return false;
    }

    @Override
    public int indexOf(Object e) {
        return 0;
    }

    @Override
    public void add(int i, Object e) {
        //从首元素找到前一个结点
       Node p=head;
       for(int j=0;j<i;j++){
          p=p.next;
       }
     //创建一个新的节点
       Node newNode=new Node(e);


        //指明新节点的直接后续
        newNode.next=p.next;

        //指明新节点的前驱
        p.next=newNode;
         size++;
    }

    @Override
    public void add(Object e) {
     add(size,e);

    }

    @Override
    public boolean addBefore(Object obj, Object e) {
        return false;
    }

    @Override
    public boolean addAfter(Object obj, Object e) {
        return false;
    }

    @Override
    public Object remove(int i) {
        Node p=head;
        for (int j=0;j<i;j++){
            p=p.next;
        }
        p.next=p.next.next;
        size--;
        return 1;
    }

    @Override
    public boolean remove(Object e) {
        Node p=head.next;
        for(int j=0;j<size;j++){
            if((int)(p.e)==(int)(e)){
                remove(j);
                System.out.println("==============");
                return true;
            }
            System.out.println(p.e);
            p=p.next;
        }
        return false;
    }

    @Override
    public Object replace(int i, Object e) {
        return null;
    }
    @Override
    public String toString() {
        StringBuilder stringBuilder=new StringBuilder();
        Node p=head.next;
        stringBuilder.append("[");
        for (int i=0;i<size;i++){
            if(i!=size-1) {
                stringBuilder.append(p.e + ",");
            }else{
                stringBuilder.append(p.e);
            }
            p=p.next;
        }
        stringBuilder.append("]");

        return stringBuilder.toString();
    }
}
